<?php
/*
 * 페이징 클래스
 *
 * Pear의 Pager를 확장
 *
 *
 * for example)
 *
 * $iTotal      = 50; //전체 레코드
 * $iPerPage    = 10; //한페이지에 표시할 수
 * new utilPager($iTotal, $iPerPage);
 *
 *
 * @package  util
 * @author gudals
 * @version  1.0
 * @date     2011. 12. 05
 */
 require_once 'Pager.php';

 class utilPager extends Pager
 {
    private $allSept;
    private $sMode;
    private $oPager;


    function utilPager($iTotal, $iPerPage, $sUrlVar='page', $aQryStr=null, $aDesign=null)
    {
        //Jumping, sliding
        $aDesign['pagingType']  = empty($aDesign['pagingType']) ? 'sliding' : $aDesign['pagingType'];
        $separator              = ($aDesign['pagingType'] == 'sliding') ? '':'';

        $aDesign        = $this->initDesign($aDesign);

        $this->allSept  = $aDesign['allsept'];
        $this->sMode    = $aDesign['pagingType'];

        $this->oPager = parent::factory(array(
                'totalItems'    => $iTotal,
                'perPage'       => $iPerPage,
                'urlVar'        => $sUrlVar,
                'delta'         => $aDesign['delta'],
                'extraVars'     => $aQryStr,
                'prevImg'       => $aDesign['prev'],
                'nextImg'       => $aDesign['next'],
                'firstPageText' => $aDesign['first'],
                'lastPageText'  => $aDesign['last'],
                'mode'          => $aDesign['pagingType'],
                'httpMethod'    => 'GET',
                'linkClass'     => $aDesign['linkcss'],
                'curPageSpanPre'=> $aDesign['curPagePreTag'],
                'curPageSpanPost'=> $aDesign['curPageEndTag'],
                'curPageLinkClassName' => 'active',
                'separator'     => $separator,
                'firstPagePre'  => '',
                'firstPagePost' => '',
                'lastPagePre'   => '',
                'lastPagePost'  => '',
                'clearIfVoid'   => false,
                'append'        => true
            ));
    }

    public function getPaging($sType = null)
    {
        $iOffset = $this->getOffset();
        $total = $this->getNumPages();
        $first = $this->getFirstPage();
        $prev  = $this->getPrevPage();
        $next  = $this->getNextPage();
        $last  = $this->getEndPage();

        if($this->sMode == 'sliding' || $this->sMode == 'jumping') {
            $all = '<div class="paging"><div class="paging_num">'.$this->getLinks('all').'</div></div>';
            $all = str_replace('&nbsp;&nbsp;&nbsp;', '&nbsp;', $all);
            $list = $this->getLinks('pages');
        } else {
            $list = $this->getSelectPage();
            $all = $first.$this->allSept.$prev.$this->allSept.$list." / <b>".$total."</b>".$this->allSept.$next.$this->allSept.$last;
        }

        return  array(
            //'first'  => $first,
            'prev'   => $prev,
            'list'   => $list,
            'total'  => $total,
            'next'   => $next,
            //'last'   => $last,
            'offset' => $iOffset,
            'all'    => $all
            );
    }

    private function getLinks($sType = 'all')
    {
        $aLink = $this->oPager->getLinks();
        return $sType ? $aLink[$sType] : $aLink;
    }

    private function getOffset()
    {
        $aOffset = $this->oPager->getOffsetByPageId();
        return ($aOffset[0]-1);
    }

    private function getNumPages()
    {
        return $this->oPager->numPages();
    }

    private function getNextPage() {
        return $this->getLinks('next');
    }

    private function getPrevPage()
    {
        return $this->getLinks('back');
    }

    private function getFirstPage()
    {
        return $this->getLinks('first');
    }

    private function getEndPage()
    {
        return $this->getLinks('last');
    }

    function getSelectPage($autoSubmit = true, $optionText = '%d', $extraAttribtes = '')
    {
        $params = array('optionText' => $optionText, 'autoSubmit' => $autoSubmit);
        return $this->oPager->getPageSelectBox($params, $extraAttributes);
    }


    private function initDesign($aDesign)
    {
        //$sFirstBtn = '<img src="http://img.cafe24.com/img1/ec_admin/icon/page_1.gif" border="0" alt="'.utilMessage::code('F0493').'" />';
        //$sPrevBtn  = '<img src="http://img.cafe24.com/img1/ec_admin/icon/page_2.gif" border="0" alt="'.utilMessage::code('F0494').'" />';
        //$sNextBtn  = '<img src="http://img.cafe24.com/img1/ec_admin/icon/page_3.gif" border="0" alt="'.utilMessage::code('F0495').'" />';
        //$sLastBtn  = '<img src="http://img.cafe24.com/img1/ec_admin/icon/page_4.gif" border="0" alt="'.utilMessage::code('F0496').'" />';

        //$sFirstBtn = '<img src="http://img.cafe24.com/img1/ec_admin/icon/page_1.gif" border="0" alt="1" />';

        //$sFirstBtn = '◀◀';
        $sPrevBtn  = '<span class="prev">이전</span>';
        $sNextBtn  = '<span class="next">다음</span>';
        //$sLastBtn  = '▶▶';


        return array(
            'pagingType'=> $aDesign['pagingType'],
            'linkcss'   => $aDesign['linkcss']  ? $aDesign['linkcss'] : '',
            'prev'      => $aDesign['prev']     ? $aDesign['prev'] : $sPrevBtn,
            'next'      => $aDesign['next']     ? $aDesign['next'] : $sNextBtn,
            'first'     => $aDesign['first']    ? $aDesign['first'] : $sFirstBtn,
            'last'      => $aDesign['last']     ? $aDesign['last'] : $sLastBtn,
            'allsept'   => $aDesign['allsept']  ? $aDesign['allsept'] : '',
            'delta'     => $aDesign['delta'],
            'curPagePreTag' => $aDesign['curPagePreTag'],
            'curPageEndTag' => $aDesign['curPageEndTag']
        );
    }
 }